package client.gui;

import java.awt.FlowLayout;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import client.Office;
import client.OfficeClient;
import client.Service;

public class OfficePanel extends DisplayPanel implements ActionListener, MouseListener {
	private static final long serialVersionUID = -8844414678998615696L;
	private final JButton officeList = new JButton("Get Office List");
	private final JButton SearchServices = new JButton("Search by Service");
	private final JTextField search = new JTextField("Search Field");
	private final JPanel displayPanel = new JPanel();
	private JScrollPane scroll;
	private ArrayList<ItemPanel> items;
	
	public OfficePanel(Service s) {
		super(s);
		items = new ArrayList<ItemPanel>();
		init();
	}
	private void init(){
		add(officeList);
		officeList.addActionListener(this);
		add(SearchServices);
		SearchServices.addActionListener(this);
		add(search);
		search.addActionListener(this);
		displayPanel.setPreferredSize(new Dimension(750, 500));
		scroll = new JScrollPane(displayPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.getVerticalScrollBar().setUnitIncrement(50);
		scroll.setPreferredSize(new Dimension(750,500));
		add(scroll);
	}
	
	public void actionPerformed(ActionEvent arg0) {
		removeAllItems();
		if(arg0.getSource() == officeList){
			ArrayList<Office> s = ((OfficeClient) myClient).getOfficeList();
			for (Office o : s) {
				OfficeInfoPanel op = new OfficeInfoPanel(o);
				op.setPreferredSize(new Dimension(750,118));
				addItem(op);
			}
			updateUI();
		} else if(arg0.getSource() == SearchServices){
			ArrayList<Office> s = ((OfficeClient) myClient).searchServices(search.getText());
			for (Office ofc : s) {
				OfficeInfoPanel op = new OfficeInfoPanel(ofc);
				op.setPreferredSize(new Dimension(750,118));
				addItem(op);
			}
			updateUI();
		}		
	}
	public void removeAllItems(){
		displayPanel.removeAll();
		items.clear();
	}

	public void addItem(ItemPanel i){
		items.add(i);
		displayPanel.add(i);
		i.setML(this);
	}
	
	public void mouseClicked(MouseEvent arg0) {
		
		if(arg0.getSource().getClass().equals(OfficeInfoPanel.class)){ // when an OfficePanel is Clicked on
			OfficeInfoPanel source = (OfficeInfoPanel)arg0.getSource();
			Vector<String> ser = ((OfficeClient) myClient).getOfficeInfo(source.getRef());
			removeAllItems();
			displayPanel.setLayout(new FlowLayout());
			addItem(source);
			for (String string : ser) {
				addItem(new ServicePanel(string));
			}
			updateUI();
		}
	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
